package com.ctakit.sdk.util;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class TUtil {
    public static <T> T getT(Object o, int i) {
        try {
            Type classType = o.getClass().getGenericSuperclass();
            if(classType instanceof  ParameterizedType ){
                Type[] types = ((ParameterizedType) classType).getActualTypeArguments();
                if (types != null && types.length >= i&&types[i] instanceof Class) {
                    return ((Class<T>) types[i]).newInstance();
                }
            }

            return null;

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Class<?> forName(String className) {
        try {
            return Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}